
const app = getApp()
const env = app.globalData.env

const isLogin=app.globalData.isLogi
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gradeList: ["大一", "大二", "大三", "大四"],
    isCreated:false,
    formData: {
      isRequireCheck: true,
      
    },
    rules: [{
        name: 'className',
        rules: {
          required: true,
          message: "请填写班级名称"
        }
      },
      {
        name: 'classNumber',
        rules: {
          required: true,
          message: "请填写班级人数"
        }
      },
      {
        name: 'grade',
        rules: {
          required: true,
          message: "请选择年级"
        }
      },
      {
        name: 'adminPhone',
        rules: [{
          required: true,
          message: "请填写电话号码"
        },{
          mobile: true, 
          message: '电话号码格式不正确'
        }]
      }
    ]
  },
// 选择年级
  selectGrade(e) {
    console.log(e)
    let index = e.detail.value
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      index: e.detail.value,
      [`formData.${field}`]: this.data.gradeList[index]
    })

  },
  switchChange(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value,
    })
  },

  submitForm(e) {
    let _this=this
    let app=getApp()
    let env=app.globalData.env
    let tableData=this.data.formData
    console.log(this.data.formData)
    this.selectComponent('#form').validate((valid, errors) => {
      console.log('valid', valid, errors)
      if (!valid) {
        const firstError = Object.keys(errors)
        if (firstError.length) {
          wx.showToast({
            icon: 'none',
            title: errors[firstError[0]].message,
          })
          this.setData({
            error: errors[firstError[0]].message
          })
          console.log(errors[firstError[0]].message)

        }
      } else {
        wx.showToast({
          title: '校验通过'
        })
        console.log("formData.createrId",_this.data.formData.createrId)
  
        wx.request({
          // url:'http://www.wantstong.com:88/api/createClass',
          url: env!=1?'http://www.localhost:88/api/createClass':'https://www.wantstong.com/api/createClass',
          method: 'POST',
          data: {
            data: tableData
          },
          success: res => {
            let classId=res.data.classId
            console.log("classId",classId)
            wx.showToast({
              title: '创建成功',
            })
            this.setData({
              classId:classId,
              isCreated:true
            })
           
          }
        })
      }
    })
  },
  formInputChange(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this=this
    wx.getStorage({
      key: 'user',
      success: (res) => {
        console.log(res)
        _this.setData({
          userName: res.data.nickName,
          formData:{
            adminName:res.data.nickName,
            createrId:res.data.userId
          }
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})